package com.lemon.cloud.author.oauth2.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.lemon.cloud.author.oauth2.domain.Authority;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public Principal getUser(Principal principal) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>");
        log.info(JSON.toJSONString(principal));
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>");

        if(principal == null) {
            List<GrantedAuthority> authorities = Lists.newArrayList();
            authorities.add(new Authority(1L, "ROLE"));
            principal = new AnonymousAuthenticationToken("111", "222", authorities);
        }

        return principal;
    }
}